// pages/mine/collect/collect.js
const app = getApp()
const util = require("../../../../utils/util.js");
var page = 1;
import tools from "../../../../utils/tools.js";
var load = true;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        collectList: [],
        showCheckbox: false,
        waitNum: 0,
        showCot: false,
        chouseMessaages: [],
        type_id: "",
        hasContent: true,
        model: false,
        onload: true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        ks.setNavigationBarTitle({
            // 页面标题
            title: options.collect,
            // 接口调用成功的回调函数
            success: res => {},
            // 接口调用失败的回调函数
            fail: res => {},
            // 接口调用结束的回调函数（调用成功、失败都会执行）
            complete: res => {}
        });
        this.data.type_id = options.id ? options.id : "0";
        this.setData({
            type_id: this.data.type_id,
        });
        util.getUrl(app.globalData.url);
    },

    // 删除收藏夹
    delete() {
        ks.showModal({
            title: '温馨提示',
            content: '确认删除收藏夹？',
            showCancel: true,
            cancelText: '取消',
            cancelColor: '#000000',
            confirmText: '确定',
            confirmColor: '#3CC51F',
            success: (result) => {
                if (result.confirm) {
                    util.request("user.php", {
                        action: "collect",
                        option: "del_dir",
                        type_id: this.data.type_id,
                        user_id: ks.getStorageSync("user_id"),
                        openid: ks.getStorageSync("openid"),
                    }, "POST").then(res => {
                        ks.navigateBack({
                            delta: 1
                        });
                    })
                }
            },
        });
    },

    onShow() {
        load = true;
        this.setData({
            onload: true,
        });
        tools.getUserCollect(this.data.type_id).then(res => {
            this.setData({
                collectList: res,
                hasContent: res.length ? true : false,
                showCot: false,
                onload: false,
            });
        });
    },

    // 删除收藏
    delCollect(e) {
        this.setData({
            model: true,
            showmove: false,
            showCot: false,
        });
    },

    // 提示框取消
    modelCancel() {
        this.setData({
            model: false,
        });
    },

    // 完成批量编辑
    done() {
        this.setData({
            showCheckbox: false,
        })
    },

    // 批量选择文章
    chouseMessage(e) {
        let chouseMessaages = e.detail.value;
        this.setData({
            chouseMessaages
        });
    },

    // 跳转
    navBtn(e) {
        let index = e.currentTarget.dataset.index;
        let data = this.data.collectList[index].value;
        let id = data.id;
        let nav = data.nav;
        let citycode = this.data.collectList[index].citycode;
        let path = data.path;
        if (path == "/pages/topicality/bsy/bsy") {
            ks.navigateTo({
                url: path + '?id=' + id + '&city=' + citycode,
            })
        } else {
            ks.navigateTo({
                url: '/pages/article/detail/detail?id=' + id + '&type=' + nav + '&city=' + citycode,
            })
        }

    },

    closeAll() {
        this.setData({
            model: false,
            showCot: false,
            showCheckbox: false,
        });
    },

    // 提示款确认删除
    modelDelete(e) {
        // 直接删除
        if (e.detail.value.delete[0]) {
            tools.deleteCollect(this.data.chouseMessaages.join(",")).then(res => {
                this.closeAll();
                this.onShow();
            })
        } else {
            // 从当亲文件夹删除
            tools.moveCollect("", this.data.chouseMessaages.join(",")).then(res => {
                this.closeAll();
                this.onShow();
            });
        }
    },

    // 打开更多功能
    more() {
        this.setData({
            showCot: true,
        })
    },

    // 关闭更多
    cancel() {
        this.setData({
            showCot: false,
        })
    },

    // 批量编辑
    editAll() {
        this.setData({
            showCheckbox: true,
        })
    },

    // 移动文章
    moveTo(e) {
        // 移入代办
        if (this.data.type_id == 9) {
            tools.moveCollect("", this.data.chouseMessaages.join(","), ).then(res => ks.showToast({
                title: '移出代办成功',
                icon: 'none',
                success: res => {
                    this.closemove();
                    this.onShow();
                }
            }));
        } else {
            tools.moveCollect(9, this.data.chouseMessaages.join(","), ).then(res => ks.showToast({
                title: '移入代办成功',
                icon: 'none',
                success: res => {
                    this.closemove();
                    this.onShow();
                }
            }));
        }
    },

    closemove() {
        this.setData({
            showCot: true,
        })
    },

    // 置顶
    top() {
        tools.top(this.data.chouseMessaages.join(",")).then(res => {
            this.closeAll();
            this.onShow();
        });
    },

    onReachBottom: function() {
        page++;
        if (load)
            tools.getUserCollect(this.data.type_id, page).then(res => {
                if (res[0]) {
                    res.forEach(item => this.data.collectList.push(item));
                    this.setData({
                        collectList: this.data.collectList,
                    });
                } else
                    load = false;
            });
    },

    onShareAppMessage: function() {

    }
})